/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.graphicals;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.*;

/**
 * @author thomas
 */
public class ToolBar extends JToolBar {
    
    private static Color _myBackground = new Color(238, 238, 238); 
    private ActionListener _myActionListener = null;
    
    public ToolBar() {
        this(SwingConstants.HORIZONTAL);
    }

    public ToolBar(int orientation) {
         super(orientation);
        
        setFloatable(false);
        setBackground(_myBackground);
    }

    /**
     * Create a button on toolbar
     * @param imageName         Name of the image (search on default image directory = /drawable/)
     * @param actionCommand     Name of the action command
     * @param toolTipText       Text if user keep cursor on button
     * @param altText           Text if image is not found
     * @param actionList        ActionListener to add dynamic interaction
     */
    public void makeButton(String imageName,
            String actionCommand,
            String toolTipText,
            String altText,
            ActionListener actionList) {

        add(makeNewButton(imageName, actionCommand, toolTipText, altText, actionList));
    }
    
    
    public JButton makeNewButton(String imageName,
            String actionCommand,
            String toolTipText,
            String altText,
            ActionListener actionList) {
        
        JButton button = new JButton();

        button.setActionCommand(actionCommand);
        button.setToolTipText(toolTipText);
        
//        // If an action listener if already setted
//        if (_myActionListener != null)
//            actionList = _myActionListener;
        button.addActionListener(actionList);
        
        // Apparences
        button.setBorderPainted(false);
        button.setBackground(_myBackground);

        // If an icon is defined for button
        if (!imageName.equals("")) {
            
            URL imagePath = getClass().getResource("/drawable/" + imageName);
            if (imagePath != null)
                button.setIcon(new ImageIcon(imagePath, altText));
            else
                button.setText(altText);
            
        // Else, we used alternative text
        } else {
            button.setText(altText);
        }
        
        return button;
    }

    /**
     * Create a textfield on toolbar
     * @param defaultText       Default text in field
     * @param actionCommand     Name of the action command (to catch an event)
     * @param columns           Lenght of the text field
     * @param actionList        ActionListener who catch evenements
     */
    public void makeTextField(String defaultText,
            String actionCommand,
            int columns,
            ActionListener actionList) {

        JTextField texte = new JTextField(defaultText);
        texte.setActionCommand(actionCommand);
        texte.setColumns(columns);
        texte.addActionListener(actionList);

        add(texte);
    }
    
    
    public JButton changeButton(JButton btn, String imageName, String altText, String actionCommand) {
        
        
        btn.setActionCommand(actionCommand);
        
        // If an icon is defined for button
        if (!imageName.equals("")) {
            
            URL imagePath = getClass().getResource("/drawable/" + imageName);
            if (imagePath != null)
                btn.setIcon(new ImageIcon(imagePath, altText));
            else
                btn.setText(altText);
            
        // Else, we used alternative text
        } else {
            btn.setText(altText);
        }
        
        return btn;
    }
}
